package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.service.IUserRoleService;
import cn.uniqueinfo.dp.service.IUserService;
import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.Result;
import cn.uniqueinfo.dp.vo.UserRoleVO;
import cn.uniqueinfo.dp.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-04 14:26:44
 * @description:用户控制器
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController<UserVO> {

    @Autowired private IUserRoleService userRoleService;

    private IUserService service;

    public UserController(IUserService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "user";
        this.tipName = "用户管理";
    }

    /** 保存角色与用户关联关系 */
    @PostMapping(value = "/ur")
    @ResponseBody
    public Result saveUserRole(@RequestParam(value = "uid") Long uid, @RequestParam(value = "rids") String ridStrs) {
        try {
            if (ridStrs == null || ridStrs.length() == 0) {
                return ResultUtil.fail();
            }
            this.userRoleService.deleteByUid(uid);
            String[] rids = ridStrs.split(",");
            for (String rid : rids) {
                UserRoleVO userRoleVO = new UserRoleVO();
                userRoleVO.setRid(Long.parseLong(rid));
                userRoleVO.setUid(uid);
                this.userRoleService.insert(userRoleVO);
            }
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.fail();
        }
    }
}
